﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business.Util;
using ZLCMS.Data.Model;
using DotNet.Common.NHibernateUtil;
using NHibernate;
using DotNet.Common.NHibernateUtil.QueryFilter;
using NHibernate.Criterion;

namespace ZLCMS.Web.SystemManage.Basics.Shop.Member
{
    public partial class CommentList : System.Web.UI.Page
    {
        protected ZLCMS.Business.DwzFrame.FrameHelper _framePage;
        public string _memberid;
        public string _membernickname;
        protected void Page_Load(object sender, EventArgs e)
        {
            _framePage = new ZLCMS.Business.DwzFrame.FrameHelper(this);

            //设置分页大小
            int pageSize = _framePage.IsAjaxPagerPostBack ? _framePage.PageSize : 20;

            //设置页码
            int pageIndex = _framePage.IsAjaxPagerPostBack ? _framePage.PageIndex : 1;
            _memberid = Request["memberid"] == null ? "" : Request["memberid"];
            _membernickname = Request["membernickname"] == null ? "" : Request["membernickname"];

            QueryFilterList filters = new QueryFilterList();
            if (!string.IsNullOrEmpty(Request["TargetKind"]))
            {
                filters.Add(new StringFilter("TargetKind", Request["TargetKind"]));
            }
            if (!string.IsNullOrEmpty(Request["TargetCode"]))
            {
                filters.Add(new StringFilter("TargetCode", Request["TargetCode"]));
            }
            if (!string.IsNullOrEmpty(_memberid))
            {
                filters.Add(new StringFilter("MemberAccount", "%" + GetMemberAccount(_memberid) + "%",StringCompairType.Like));
            }
            if (!string.IsNullOrEmpty(_membernickname))
            {
                filters.Add(new StringFilter("MemberAccount", "%" + GetMemberAccount(_membernickname) + "%", StringCompairType.Like));
            }
            tb_search_memberid.Text = _memberid;
            tb_search_membernickname.Text = _membernickname;
            string hql = " from MemberComment where 1=1 ";
            foreach (DataFilter filter in filters)
            {
                hql += " and " + filter.GetSection();
            }
            using (NHibernate.ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                IQuery query = null;

                query = session.CreateQuery("select count(*) " + hql);
                filters.SetUp(query);

                //设置记录总数
                _framePage.RecordCount = Convert.ToInt32(query.UniqueResult());

                query = session.CreateQuery(hql + " order by Id desc");
                filters.SetUp(query);

                //设置数据源
                repeater1.DataSource = query.SetFirstResult((pageIndex - 1) * pageSize)
                    .SetMaxResults(pageSize)
                    .List<MemberComment>();
            }

            repeater1.DataBind();
        }

        protected int GetProductId(object productNo)
        {
            using(ISession session = SessionFactoryHelper.CreateSession())
            {
                return session.CreateSQLQuery("select id from shop_product_info where product_no=:no")
                    .SetParameter("no", productNo)
                    .UniqueResult<int>();
            }

        }

        protected object GetReplyCounter(object id)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                return session.CreateSQLQuery("select count(*) from member_comment where target_kind='COMMENT' and target_code=:code")

                    .SetParameter("code", id.ToString())
                    .UniqueResult();
            }
        }

        #region 获取会员真实姓名 + protected MemberInfo GetMemberRealName(object account)
        /// <summary>
        /// 获取会员真实姓名
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        protected MemberInfo GetMemberRealName(object account)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                MemberInfo member = session.CreateCriteria<MemberInfo>().Add(Expression.Eq("OwnerAccount", UserKit.GetUserAccountFailLogin())).Add(Expression.Eq("AccountId", account.ToString())).UniqueResult<MemberInfo>();

                return member == null ? new MemberInfo() : member;
            }
        }
        #endregion

        #region 获取会员account + protected string GetMemberAccount(object telOrName)
        /// <summary>
        /// 获取会员account
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        protected string GetMemberAccount(object telOrName)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                return session.CreateSQLQuery("select account_id from member_info where owner_account='admin' and (telephone=:code or nick_name=:code)")

                       .SetParameter("code", telOrName.ToString())
                       .UniqueResult<string>();
            }
        }
        #endregion
    }
}